/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.starter.idempotent.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import top.continew.starter.idempotent.service.IdempotentService;
import top.continew.starter.idempotent.service.impl.MemoryIdempotentServiceImpl;

/**
 * 内存定时任务配置类
 *
 * @version 1.0
 * @Author loach
 * @Date 2025-03-07 22:26
 * @Package top.continew.starter.idempotent.config.MemoryIdempotentSchedulingConfig
 */
@Configuration
@ConditionalOnMissingBean(name = "redisIdempotentService") // 当 Redis Bean 不存在时启用
@EnableScheduling
public class MemoryIdempotentSchedulingConfig {

    @Autowired
    private IdempotentService idempotentService;

    @Scheduled(fixedRateString = "${idempotent.clean-interval:60000}")
    public void cleanExpiredKeys() {
        if (idempotentService instanceof MemoryIdempotentServiceImpl) {
            ((MemoryIdempotentServiceImpl)idempotentService).cleanExpiredKeys();
        }
    }
}
